/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-08-02 09:01:32
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-08-16 14:53:29
 * @FilePath: \JS32T031_SDK\User\task\driver_board_protocol_analysis.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */
#include "board.h"
#include "driver_board_protocol_analysis.h"
#include "device_station_data.h"

// 跑步机数据包解析
void treadmill_data_analysis(unsigned char *pBuf, int len)
{
    DRIVER_BOARD_DATA_T driver_data; // 设备
    unsigned int flags = pBuf[0] + (pBuf[1] << 8);
    driver_data.flags = flags;
    int dataIndex = 2;
    unsigned int flagBit = 0x01;
    for (int i = 0; i <= 5; i++)
    {
        flagBit = 0x01 << i;
        if ((flags & flagBit) == flagBit)
        { // 遍历flag每一位
            switch (i)
            {
            case 0: // 转速
                if (len >= (dataIndex + 2))
                {
                    unsigned int data_value = 0x0000;
                    data_value = data_value | pBuf[dataIndex + 1];
                    data_value = data_value << 8;
                    data_value = data_value | pBuf[dataIndex];
                    driver_data.rotational_speed = data_value;
                    dataIndex += 2;
                }
                break;
            case 1: // 步数
                if (len >= (dataIndex + 2))
                {
                    unsigned int data_value = 0x0000;
                    data_value = data_value | pBuf[dataIndex + 1];
                    data_value = data_value << 8;
                    data_value = data_value | pBuf[dataIndex];
                    driver_data.rotational_speed = data_value;
                    dataIndex += 2;
                }
                break;
            case 2: // 错误代码

                if (len >= (dataIndex + 2))
                {
                    unsigned int data_value = 0x0000;
                    data_value = data_value | pBuf[dataIndex + 1];
                    data_value = data_value << 8;
                    data_value = data_value | pBuf[dataIndex];
                    driver_data.rotational_speed = data_value;
                    dataIndex += 2;
                }
                break;
            case 3: // 电机输入功率
                if (len >= (dataIndex + 2))
                {
                    unsigned int data_value = 0x0000;
                    data_value = data_value | pBuf[dataIndex + 1];
                    data_value = data_value << 8;
                    data_value = data_value | pBuf[dataIndex];
                    driver_data.rotational_speed = data_value;
                    dataIndex += 2;
                }
                break;
            case 4: // 电机输出功率
                if (len >= (dataIndex + 2))
                {
                    unsigned int data_value = 0x0000;
                    data_value = data_value | pBuf[dataIndex + 1];
                    data_value = data_value << 8;
                    data_value = data_value | pBuf[dataIndex];
                    driver_data.rotational_speed = data_value;
                    dataIndex += 2;
                }
                break;
            case 5: // Iq
                if (len >= (dataIndex + 2))
                {
                    unsigned int data_value = 0x0000;
                    data_value = data_value | pBuf[dataIndex + 1];
                    data_value = data_value << 8;
                    data_value = data_value | pBuf[dataIndex];
                    driver_data.rotational_speed = data_value;
                    dataIndex += 2;
                }
                break;
            default:
                break;
            }
        }
    }
    send_app_treadmill_data_analysis(&driver_data);
}

char protocol_analysis(unsigned char *data_value, int len)
{
    if (len <= 6) // 协议最短长度 限制
    {
        return ERROR;
    }
    // 帧头 and 针尾 判断
    if (data_value[0] == DRIVER_FRAME_HEADER && data_value[len - 1] == DRIVER_FRAME_TAIL)
    {
        uint8_t crc = 0;
        for (uint8_t crc_len = 0; crc_len < len - 3; crc_len++)
        {
            crc += data_value[crc_len + 1];
        }
        if (data_value[len - 2] == crc)
        {
            // printf("crc SUCCESS ");
            if (data_value[2] == DRIVER_TREADMILL_DATA)
            {
                treadmill_data_analysis(&data_value[4], len - data_value[3]);
            }

            return SUCCESS;
        }
    }
    return ERROR;
}
void driver_board_task(void)
{
    if (rx_finish_get() == true) // 进入空闲中断
    {
        u8 rx_buff[UART_BUF_LEN];
        u8 rx_buff_len = rx_buf_get(rx_buff);

#if 0 /*output uart log*/
        printf("\r\nlen: %u ", rx_buff_len);
        for (size_t i = 0; i < rx_buff_len; i++)
        {
            printf(" 0x%x ", rx_buff[i]);
        }
#endif
        /*协议解析*/
        protocol_analysis(rx_buff, rx_buff_len);
        rx_finish_clr(); // 清除标志
    }
}
